package com.keya.pacs;

import cn.hutool.core.util.ObjectUtil;
import com.keya.base.constant.StatusCode;
import com.keya.base.exception.BusinessException;
import com.keya.base.output.PageData;
import com.keya.base.output.ResultData;
import com.keya.device.Device;
import com.keya.device.DeviceService;
import com.keya.dicomtask.DicomTask;
import com.keya.dicomtask.DicomTaskService;
import com.keya.features.job.MonitorDiskJob;
import com.keya.pull.PullStudyRecord;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 远程pacs操作
 *
 * @author : xf.he
 * @version : 1.0 2023/11/2
 * @since : 1.0
 **/
@Slf4j
@RestController
public class PacsController {

    @Resource
    PacsService pacsService;
    @Resource
    MonitorDiskJob monitorDiskJob;
    @Resource
    DeviceService deviceService;
    @Resource
    DicomTaskService dicomTaskService;

    @GetMapping("/select/{deviceId}")
    public PageData select(@RequestParam(name = "offset", defaultValue = "0") Integer offset,
                           @RequestParam(name = "limit", defaultValue = "10") Integer limit,
                           @PathVariable("deviceId") Integer deviceId, boolean pullFilterDateField) throws Exception {
        List<PullStudyRecord> list = pacsService.list(deviceId, pullFilterDateField);
        if (ObjectUtil.isEmpty(list)) {
            return null;
        }
        return PageData.build((long) list.size(), list.subList(offset, limit));
    }

    @GetMapping("/find/{deviceId}/{studyInstanceUID}")
    public ResultData findSCUStudy(@PathVariable("deviceId") Integer deviceId,
                                   @PathVariable("studyInstanceUID") String studyInstanceUID,
                                   @RequestParam(value = "filter", required = false) String[] filters) {
        return pacsService.findScu(deviceId, filters, studyInstanceUID);
    }

    @GetMapping("/find/{deviceId}")
    public ResultData findSCUSeries(@PathVariable("deviceId") String deviceId,
                                    @RequestParam(value = "filter", required = false) String[] filters) {
        return pacsService.findScuStudy(Integer.parseInt(deviceId), filters);
    }

    @GetMapping("/move/{deviceId}/{studyInstanceUID}/{seriesInstanceUID}")
    public ResultData moveSCU(@PathVariable("deviceId") Integer deviceId,
                              @PathVariable("studyInstanceUID") String studyInstanceUID,
                              @PathVariable("seriesInstanceUID") String seriesInstanceUID) throws Exception {
        return pacsService.moveScu(deviceId, studyInstanceUID, seriesInstanceUID);
    }


    @PostMapping("/push/{id}")
    public ResultData add(@PathVariable("id") Integer id,
                          @RequestParam(required = false, value = "deviceId") Integer deviceId,
                          @RequestParam(required = false, value = "aetitle") String aetitle,
                          @RequestParam(required = false, value = "ip") String ip,
                          @RequestParam(required = false, value = "port") String port) throws Exception {
        if (monitorDiskJob.isDiskIsFull()) {
            log.error("硬盘剩余空间不足，dicomtask停止运行！！！！");
            throw new BusinessException(StatusCode.DISK_FULL, "硬盘剩余空间不足，dicomtask停止运行！！！！");
        }
        Device device;
        if (StringUtils.isNotEmpty(aetitle) && StringUtils.isNotEmpty(ip) && StringUtils.isNotEmpty(port)) {
            device = new Device();
            device.setAetitle(aetitle);
            device.setIp(ip);
            device.setPort(port);
        } else {
            if (ObjectUtil.isEmpty(deviceId)) {
                throw new BusinessException(-1, "设备id不能为空");
            }
            device = deviceService.getById(deviceId);
        }
        DicomTask dicomTask = dicomTaskService.getById(id);
        return pacsService.storeScu(device, dicomTask.getPath());
    }

}
